home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xcalc / xcalc.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  11KB  |  389 lines

  1. /*
  2.  * $XConsortium: xcalc.c,v 1.15 91/02/16 19:39:37 converse Exp $
  3.  *
  4.  * xcalc.c  -  a hand calculator for the X Window system
  5.  * 
  6.  * Copyright 1989 by the Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided 
  10.  * that the above copyright notice appear in all copies and that both that 
  11.  * copyright notice and this permission notice appear in supporting 
  12.  * documentation, and that the name of M.I.T. not be used in advertising
  13.  * or publicity pertaining to distribution of the software without specific, 
  14.  * written prior permission. M.I.T. makes no representations about the 
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  20.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  21.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  22.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  *
  26.  *     Original Author:  John H. Bradley, University of Pennsylvania
  27.  *                (bradley@cis.upenn.edu)
  28.  *                  March, 1987
  29.  *  RPN mode added and port to X11 by Mark Rosenstein, MIT Project Athena
  30.  *  Rewritten to be an Xaw and Xt client by Donna Converse, MIT X Consortium
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <math.h>
  35. #include <signal.h>
  36. #include <X11/Intrinsic.h>
  37. #include <X11/StringDefs.h>
  38. #include <X11/Xatom.h>
  39. #include <X11/Shell.h>
  40. #include <X11/Xaw/Cardinals.h>
  41. #include <X11/Xaw/Form.h>
  42. #include <X11/Xaw/Label.h>
  43. #include <X11/Xaw/Command.h>
  44. #include <X11/Xaw/Toggle.h>
  45. #include <X11/cursorfont.h>
  46. #include "xcalc.h"
  47. #include "actions.h"
  48.  
  49. #ifndef IEEE
  50. extern signal_t fperr();
  51. extern signal_t illerr();
  52. #endif
  53.  
  54. /*
  55.  *    global data
  56.  */
  57. int    rpn = 0;        /* Reverse Polish Notation (HP mode) flag */
  58. #define LCD_STR_LEN    32
  59. char    dispstr[LCD_STR_LEN];        /* string to show up in the LCD */
  60. Atom    wm_delete_window;        /* see ICCCM section 5.2.2 */
  61.  
  62. /*
  63.  *    local data 
  64.  */
  65. static Display    *dpy = NULL;        /* connection to the X server */
  66. static Widget    toplevel=NULL;      /* top level shell widget */
  67. static Widget   calculator=NULL;    /* an underlying form widget */
  68. static Widget    LCD = NULL;        /* liquid crystal display */
  69. static Widget    ind[6];            /* mode indicators in the screen */
  70. static char    selstr[LCD_STR_LEN]; /* storage for selections from the LCD */
  71.                     /* checkerboard used in mono mode */
  72. static XtAppContext xtcontext;        /* Toolkit application context */
  73. #define check_width 16
  74. #define check_height 16
  75. static unsigned char check_bits[] = {
  76.    0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,
  77.    0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa,
  78.    0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa};
  79.  
  80. /*    command line options specific to the application */
  81. static XrmOptionDescRec Options[] = {
  82. {"-rpn",    "rpn",        XrmoptionNoArg,        (XtPointer)"on"},
  83. {"-stipple",    "stipple",    XrmoptionNoArg,        (XtPointer)"on"}
  84. };
  85.  
  86. /*    resources specific to the application */
  87. static struct resources {
  88.     Boolean    rpn;        /* reverse polish notation (HP mode) */
  89.     Boolean    stipple;    /* background stipple */
  90.     Cursor    cursor;
  91. } appResources;
  92.  
  93. #define offset(field) XtOffsetOf(struct resources, field)
  94. static XtResource Resources[] = {
  95. {"rpn",        "Rpn",        XtRBoolean,    sizeof(Boolean),
  96.      offset(rpn),    XtRImmediate,    (XtPointer) False},
  97. {"stipple",    "Stipple",    XtRBoolean,    sizeof(Boolean),
  98.      offset(stipple),    XtRImmediate,    (XtPointer) False},
  99. {"cursor",    "Cursor",    XtRCursor,    sizeof(Cursor),
  100.      offset(cursor),    XtRCursor,    (XtPointer)NULL}
  101. };
  102. #undef offset
  103.  
  104.  
  105. void main(argc, argv)
  106.     int        argc;
  107.     char    **argv;
  108. {
  109.     Arg        args[3];
  110.  
  111.     void create_calculator();
  112.     void Quit(), Syntax();
  113.  
  114.  
  115.     toplevel = XtAppInitialize(&xtcontext, "XCalc", Options, XtNumber(Options),
  116.                    &argc, argv, NULL, NULL, 0);
  117.     if (argc != 1) Syntax(argc, argv);
  118.     
  119.     XtSetArg(args[0], XtNinput, True);
  120.     XtSetValues(toplevel, args, ONE);
  121.  
  122.     XtGetApplicationResources(toplevel, (XtPointer)&appResources, Resources,
  123.                   XtNumber(Resources), (ArgList) NULL, ZERO);
  124.  
  125.     create_calculator(toplevel);
  126.  
  127.     XtAppAddActions(xtcontext, Actions, XtNumber(Actions));
  128.  
  129.     XtOverrideTranslations(toplevel, 
  130.        XtParseTranslationTable("<Message>WM_PROTOCOLS: quit()\n"));
  131.  
  132.     XtRealizeWidget(toplevel);
  133.  
  134.     dpy = XtDisplay(toplevel);
  135.     wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  136.     (void) XSetWMProtocols(dpy, XtWindow(toplevel), &wm_delete_window, 1);
  137.     XDefineCursor(dpy, XtWindow(toplevel), appResources.cursor);
  138.  
  139.     if (appResources.stipple || (CellsOfScreen(XtScreen(toplevel)) <= 2))
  140.     {
  141.     Screen    *screen = XtScreen(toplevel);
  142.     Pixmap    backgroundPix;
  143.  
  144.     backgroundPix = XCreatePixmapFromBitmapData
  145.         (dpy, XtWindow(toplevel),
  146.          (char *)check_bits, check_width, check_height,
  147.          WhitePixelOfScreen(screen), BlackPixelOfScreen(screen),
  148.          DefaultDepthOfScreen(screen));
  149.     XtSetArg(args[0], XtNbackgroundPixmap, backgroundPix);
  150.     XtSetValues(calculator, args, ONE);
  151.     }
  152.  
  153. #ifndef IEEE
  154.     signal(SIGFPE,fperr);
  155.     signal(SIGILL,illerr);
  156. #endif
  157.     ResetCalc();
  158.     XtAppMainLoop(xtcontext);
  159. }
  160.  
  161. void create_calculator(shell)
  162.     Widget    shell;
  163. {
  164.     void create_display();
  165.     void create_keypad();
  166.  
  167.     rpn = appResources.rpn;
  168.     calculator = XtCreateManagedWidget(rpn ? "hp" : "ti", formWidgetClass,
  169.                        shell, (ArgList) NULL, ZERO);
  170.     create_display(calculator);
  171.     create_keypad(calculator);
  172.     XtSetKeyboardFocus(calculator, LCD);
  173. }
  174.  
  175. /*
  176.  *    Do the calculator data display widgets.
  177.  */
  178. void create_display(parent)
  179.     Widget    parent;
  180. {
  181.     Widget    bevel, screen;
  182.     static Arg    args[] = {
  183.         {XtNborderWidth, (XtArgVal)0},
  184.         {XtNjustify, (XtArgVal)XtJustifyRight}
  185.     };
  186.  
  187.     /* the frame surrounding the calculator display */
  188.     bevel = XtCreateManagedWidget("bevel", formWidgetClass, parent,
  189.                   (ArgList) NULL, ZERO);
  190.  
  191.     /* the screen of the calculator */
  192.     screen = XtCreateManagedWidget("screen", formWidgetClass, bevel,
  193.                    (ArgList) NULL, ZERO);
  194.  
  195.     /* M - the memory indicator */
  196.     ind[XCalc_MEMORY] = XtCreateManagedWidget("M", labelWidgetClass, screen,
  197.                     args, XtNumber(args));
  198.  
  199.     /* liquid crystal display */
  200.     LCD = XtCreateManagedWidget("LCD", toggleWidgetClass, screen, args,
  201.                 XtNumber(args));
  202.  
  203.     /* INV - the inverse function indicator */
  204.     ind[XCalc_INVERSE] = XtCreateManagedWidget("INV", labelWidgetClass, 
  205.                      screen, args, XtNumber(args));
  206.  
  207.     /* DEG - the degrees switch indicator */
  208.     ind[XCalc_DEGREE] = XtCreateManagedWidget("DEG", labelWidgetClass, screen,
  209.                     args, XtNumber(args));
  210.  
  211.     /* RAD - the radian switch indicator */
  212.     ind[XCalc_RADIAN] = XtCreateManagedWidget("RAD", labelWidgetClass, screen,
  213.                     args, XtNumber(args));
  214.  
  215.     /* GRAD - the grad switch indicator */
  216.     ind[XCalc_GRADAM] = XtCreateManagedWidget("GRAD", labelWidgetClass, screen,
  217.                     args, XtNumber(args));
  218.  
  219.     /* () - the parenthesis indicator */
  220.     ind[XCalc_PAREN] = XtCreateManagedWidget("P", labelWidgetClass, screen,
  221.                          args, XtNumber(args));
  222. }
  223.  
  224. /*
  225.  *    Do all the buttons.  The application defaults file will give the
  226.  *    default button placement, default button labels, and default 
  227.  *      actions connected to the buttons.  The user can change any of 
  228.  *      these defaults in an environment-specific resource file.
  229.  */
  230.  
  231. void create_keypad(parent)
  232.     Widget    parent;
  233. {
  234.     static char    *Keyboard[] = {
  235.     "button1", "button2", "button3", "button4", "button5",
  236.     "button6", "button7", "button8", "button9", "button10",
  237.     "button11","button12","button13","button14","button15",
  238.     "button16","button17","button18","button19","button20",
  239.     "button21","button22","button23","button24","button25",
  240.     "button26","button27","button28","button29","button30",
  241.     "button31","button32","button33","button34","button35",
  242.     "button36","button37","button38","button39","button40"
  243.     };
  244.     register int i;
  245.     int         n = XtNumber(Keyboard);
  246.  
  247.     if (appResources.rpn) n--;     /* HP has 39 buttons, TI has 40 */
  248.  
  249.     for (i=0; i < n; i++)
  250.     XtCreateManagedWidget(Keyboard[i], commandWidgetClass, parent,
  251.                   (ArgList) NULL, ZERO);
  252. }
  253.  
  254. /*
  255.  *    Miscellaneous utility routines that interact with the widgets.
  256.  */
  257.  
  258. /*
  259.  *     called by math routines to write to the liquid crystal display.
  260.  */
  261. void draw(string)
  262.     char    *string;
  263. {
  264.     Arg    args[1];
  265.  
  266.     XtSetArg(args[0], XtNlabel, string);
  267.     XtSetValues(LCD, args, ONE);
  268. }
  269. /*
  270.  *    called by math routines to turn on and off the display indicators.
  271.  */
  272. void setflag(indicator, on)
  273.     int        indicator;
  274.     Boolean    on;
  275. {
  276.     if (on) XtMapWidget(ind[indicator]);
  277.     else XtUnmapWidget(ind[indicator]);
  278. }
  279.  
  280. /*
  281.  *    ring the bell.
  282.  */
  283. void ringbell()
  284. {
  285.     XBell(dpy, 0);
  286. }
  287.  
  288. /*
  289.  *    die.
  290.  */
  291. void Quit()
  292. {
  293.     extern void exit();
  294.     XtDestroyApplicationContext(xtcontext);
  295.     exit(0);
  296. }
  297.  
  298. /*  
  299.  *    recite and die.
  300.  */
  301. void Syntax(argc, argv)    
  302.     int        argc;
  303.     char    **argv;
  304. {
  305.     register int i;
  306.     extern void exit();
  307.     (void) fprintf(stderr, "%s: unknown options:", argv[0]);
  308.     for (i=1; i <argc; i++)
  309.     (void) fprintf(stderr, " %s", argv[i]);
  310.     (void) fprintf(stderr, "\n\n");
  311.     (void) fprintf(stderr, "Usage:  %s", argv[0]);
  312.     for (i=0; i < XtNumber(Options); i++)
  313.     (void) fprintf(stderr, " [%s]", Options[i].option);
  314.     (void) fprintf(stderr, "\n");
  315.     XtDestroyApplicationContext(xtcontext);
  316.     exit(1);
  317. }
  318.  
  319. /*    
  320.  * I use actions on the toggle widget to support selections.  This
  321.  * means that the user may not do a partial selection of the number
  322.  * displayed in the `liquid crystal display.'  Copying numbers into
  323.  * the calculator is also not supported.  So all you can do is copy
  324.  * the entire number from the calculator display.
  325.  */
  326.  
  327. /*ARGSUSED*/
  328. Boolean convert(w, selection, target, type, value, length, format)
  329.     Widget    w;
  330.     Atom    *selection;
  331.     Atom    *target;
  332.     Atom    *type;
  333.     XtPointer    *value;
  334.     unsigned long    *length;
  335.     int        *format;
  336. {
  337.     if (*target == XA_STRING)
  338.     {
  339.     *type = XA_STRING;
  340.     *length = strlen(dispstr);
  341.     strncpy(selstr, dispstr, (int)(*length));
  342.     *value = selstr;
  343.     *format = 8;
  344.     return True;
  345.     }
  346.     return False;
  347. }
  348.  
  349. /*
  350.  * called when xcalc loses ownership of the selection.
  351.  */
  352. /*ARGSUSED*/
  353. void lose(w, selection)
  354.     Widget    w;
  355.     Atom    *selection;
  356. {
  357.     XawToggleUnsetCurrent(LCD);
  358. }
  359.  
  360. /*
  361.  * called when some other client got the selection.
  362.  */
  363. /*ARGSUSED*/
  364. void done(w, selection, target)
  365.     Widget    w;
  366.     Atom    *selection;
  367.     Atom    *target;
  368. {
  369.     selstr[0] = '\0';
  370. }
  371.  
  372. /*
  373.  * called by the selection() action routine, in response to user action.
  374.  */
  375. void do_select(time)
  376.     Time    time;
  377. {
  378.     Boolean    state;
  379.     Arg        args[1];
  380.  
  381.     XtSetArg(args[0], XtNstate, &state);
  382.     XtGetValues(LCD, args, 1);
  383.  
  384.     if (state)
  385.         XtOwnSelection(LCD, XA_PRIMARY, time, convert, lose, done);
  386.     else
  387.     selstr[0] = '\0';
  388. }
  389.